34. Making Salts

Making Salts

Question:

Start Quiz:

import random
import string

# implement the function make_salt() that returns a string of 5 random
# letters use python's random module.
# Note: The string package might be useful here.

def make_salt():
    ###Your code here

User's Answer:

(Note: The answer done by the user is not guaranteed to be correct)

import random
import string

# implement the function make_salt() that returns a string of 5 random
# letters use python's random module.
# Note: The string package might be useful here.

def make_salt():
    salty=''
    for x in range(5):
        salty+=random.choice(string.ascii_letters)
    return salty

print make_salt()
Solution:

INSTRUCTOR NOTE:

Documentation for list comprehensions

Documentation for the string method join

Documentation for list comprehensions. In this case, we're using it to generate a list of 5 random characters.
Documentation for the string method join. Here we're using it on an empty string, and passing in our list of 5 random characters. This concatenates the list of characters with the empty string inserted between each element of the list. Since the empty string doesn't take up any space, this is really just a shorthand for concatenating all the elements in the list.